﻿@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@model WebshopHPWcore.Models.HistoryViewModels.HistoryViewModel


@using System.Linq;

@{
    ViewData["Title"] = "Beste klanten";
}

@functions
{
    public string GetAntiXsrfRequestToken()
    {
        return Xsrf.GetAndStoreTokens(Context).RequestToken;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<h2>Beste klanten</h2>
<div><a asp-area="" asp-controller="AdminStatistics" asp-action="Index"><< Terug naar overzichten</a></div>


<table id="table1" class="table">
    <thead>
        <tr>
            <th>
                Gebruikersnaam
            </th>
            <th>


                <input type="button" id="button1" value=" Aantal gekochte auto's" class="btn btn-default">
            </th>
            <th>


                <input type="button" id="button2" value=" Totale uitgave" class="btn btn-default">
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.HistoryItems.GroupBy(q => q.userid).OrderBy(x => x.Key).Select(x => x.Key).Reverse().Distinct())
        {
            int Count = 0;
            decimal TotalPrice = 0;
            string Username = "";
            <tr>
                <td>
                    @foreach (var user in Model.orderdetail.Where(x => x.Userid == item).Select(x => x.Email).Distinct())
                {
                    Username = user;
                }
                    <p>@Username</p>
                </td>

                <td>
                    @foreach (var count in Model.HistoryItems.Where(x => x.userid == item).Select(x => x.carid).Distinct())
                {
                    Count += 1;
                }
                    <p>@Count</p>
                </td>
                <td>
                    
                    @foreach (var totalprice in Model.HistoryItems.Where(x => x.userid == item).Select(x => x.Price))
                {
                    TotalPrice += totalprice;
                }<p>@TotalPrice</p>
                </td>
            </tr>
        }

    </tbody>
</table>

<script>
    $(function () {
        function sortByUserName(row1, row2) {
            var v1, v2;

            v1 = $(row1).find("td:eq(0)").text();
            v2 = $(row2).find("td:eq(0)").text();
            // for numbers you can simply return a-b instead of checking greater/smaller/equal
            return v2 - v1;
        }
        function sortByCarsSold(row1, row2) {
            var v1, v2;

            v1 = $(row1).find("td:eq(1)").text();
            v2 = $(row2).find("td:eq(1)").text();
            // for numbers you can simply return a-b instead of checking greater/smaller/equal
            return v2 - v1;
        }
        function sortByTotalPrice(row1, row2) {
            var v1, v2;
            v1 = $(row1).find("td:eq(2)").text();
            v2 = $(row2).find("td:eq(2)").text();
            // for numbers you can simply return a-b instead of checking greater/smaller/equal
            return v2 - v1;
        }

        function sortByColumn3And5(row1, row2) {
            var v1, v2, r;
            v1 = $(row1).find("td:eq(2)").text();
            v2 = $(row2).find("td:eq(2)").text();
            r = v1 - v2;
            if (r === 0) {
                // we have a tie in column 1 values, compare column 2 instead
                v1 = $(row1).find("td:eq(4)").text();
                v2 = $(row2).find("td:eq(4)").text();
                if (v1 < v2) {
                    r = -1;
                } else if (v1 > v2) {
                    r = 1;
                } else {
                    r = 0;
                }
            }
            return r;
        }
        $("#button1, #button2").on("click", function () {
            var rows = $("#table1 tbody tr").detach().get();
            switch (this.id) {
                case "button1":
                    rows.sort(sortByCarsSold);
                    break;
                case "button2":
                    rows.sort(sortByTotalPrice);
                    break;
            }
            $("#table1 tbody").append(rows);
        });
    });
</script>